package Offer2018;

/**
 * 查看链表的倒数第K个结点
 * Created by 雷文康 on 2018/3/4.
 */
public class Test15 {
    public static ListNode findLastK(ListNode head, int K) {
        if (head == null) {
            return null;
        }
        ListNode p1 = head;
        ListNode p2 = head;
        for (int i = 1; i < K; i++) {
            if (p1.next == null) {
                return null;
            }
            p1 = p1.next;
        }
        while (p1.next != null && p2.next != null) {
            p1 = p1.next;
            p2 = p2.next;
        }
        return p2;
    }
}
